// notifications
$.pnotify.defaults.styling = "bootstrap";
$.pnotify.defaults.history = false;

var stack_bottomright = {"dir1": "up", "dir2": "left", "firstpos1": 20, "firstpos2": 20};

function popNotification(notification) {
    $.pnotify({
        type: notification.type,
        title: notification.title,
        text: notification.text,
        addclass: 'stack-bottomright',
        stack: stack_bottomright
    });
}

$(document).ready(function () {
    // methods
    $.fn.disable = function () {
        this.attr('disabled', 'disabled');
    };
    $.fn.enable = function () {
        this.removeAttr('disabled');
    };

    $.fn.isInitialValueSet = function () {
        return this.val().replace(/' {2,}'/g, ' ') == this.attr('initial-value');
    };

    $.fn.getLastUrlToken = function () {
        return this.val().match(/\/([^\/]+)[\/]?$/)[1];
    };

    $.fn.isBlank = function () {
        return $.trim(this.val()) == '';
    };


    var modalButtons = $('.modal .btn, .modal .close');

    // make modals appear with animation and disappear quickly
    var _modals = $('.modal');
    _modals.on('hide', function () {
        $(this).removeClass('fade');
    });
    _modals.on('show', function () {
        $(this).addClass('fade');
        modalButtons.unbind('click', false);
        modalButtons.enable();
    });

    _modals.on('shown', function () {
        $(this).find('.btn.btn-primary').first().focus();
    });

    $('.modal .btn-primary').click(function () {
        modalButtons.disable();
        modalButtons.bind('click', false);
        $(this).siblings('.modal-indicator').removeClass('hide');
    });

    var updateAllProjectsModal = $('#update-all-projects-modal');
    var removeProjectModal = $('#delete-project-modal');

    // navigation menu related statements
    $('a[class=delete-project-a]').click(function (evt) {
        evt.preventDefault();

        var anchor = $(this);
        removeProjectModal.find('div.modal-body b.project-b').text(anchor.attr('project-name'));
        removeProjectModal.find('div.modal-footer a.btn-primary').attr('href', anchor.attr('href'));
        removeProjectModal.modal();
    });

    var removeTaskModal = $('#delete-task-modal');
    $('a[class=delete-task-a]').click(function (evt) {
        evt.preventDefault();

        var anchor = $(this);
        removeTaskModal.find('div.modal-body b.task-b').text(anchor.attr('task-name'));
        removeTaskModal.find('div.modal-body b.project-b').text(anchor.attr('project-name'));
        removeTaskModal.find('div.modal-footer a.btn-primary').attr('href', anchor.attr('href'));
        removeTaskModal.modal();
    });

    $('#update-all-projects').click(function (evt) {
        evt.preventDefault();
        updateAllProjectsModal.modal();
    });

    // merged filter
    $('#merged-filter').change(function () {
        $('#merged-filter-form').submit();
    })
});
